/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.demister.sgt.cmd;

import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author Rock
 */
public class ColorCommand extends GraphicsCommand {

    private Color newColor;
    private Color oldColor;

    public ColorCommand() {
        this.name = "Color";
    }

    public ColorCommand(Graphics graphics, Color newColor) {
        super(graphics, "Color");
        this.newColor = newColor;
    }

    @Override
    public boolean execute() {
        if (newColor == null) {
            return false;
        }
        oldColor = graphics.getColor();
        graphics.setColor(newColor);
        return true;
    }

    @Override
    public void undo() {
        if (oldColor == null) {
            return;
        }
        graphics.setColor(oldColor);
        oldColor = null;
    }

    @Override
    public String getCode() {
        return String.format("g.setColor(new Color(%d, true));", newColor.getRGB());
    }

    public Color getColor() {
        return newColor;
    }

    public void setColor(Color newColor) {
        this.newColor = newColor;
    }
}